<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/14
 * Time: 10:36
 */

namespace app\index\controller;


use think\Controller;
use think\Loader;
use think\Request;
use think\Response;
use think\Url;
use think\Db;
class Erweima extends Controller
{
    /**
     * 二维码
     */
    public function create()
    {
        if(Request::instance()->isPost()){
            $upd_url = $this->uploadOne();//上传图片名称;
            Loader::import('phpqrcode.phpqrcode', EXTEND_PATH);
            $url = Request::instance()->domain() . "{$upd_url}.jpg";
            $domain = Request::instance()->domain();
            $object = new \QRcode();
            $path = ROOT_PATH . 'public/erweima/'.$upd_url;//二维码保存图片路径
            //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件
            //$img_url = Request::instance()->domain().'/erweima/'.date('Ymd').'/'.$upd_url;//二维码内容链接 上传图片的链接
            $img_url = Request::instance()->domain().Url::build('view',['id' => $upd_url]);//二维码内容链接 上传图片的链接
            $ret = $object->png($img_url, $path, 10,10 , 2);
            Db::name('erweima')->insert(['ymd' => date('Ymd'),'pic_url' => $upd_url]);

            $content  = '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
            $content .= '<style> img {vertical-align:middle; width:auto; max-width:95%;} </style>';
            $content .= "<img title='24' src=/erweima/"."{$upd_url}>";
            $content .= "<title> 123</title>";
            $content .= "<div style='text-align: center;'><a href='$domain/erweima/$upd_url' style='text-align: center;margin-top: 20px;'>二维码地址链接</a></div>";
            return Response::create($content, 'html');
        }else{
            return view();
        }



    }

    public function view($id){

       // $date = date('Ymd');
        //$date = 20190514;
        $d = Db::name('erweima')->where('pic_url',$id)->value('ymd');

        if(empty($d)){
            $date = 20190514;
        }else{
            $date = $d;
        }
        $content  = '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= '<style> img {vertical-align:middle; width:auto; max-width:100%;} body{background: #CCCCCC} *{padding:0;margin: 0 } </style>';
        $content .= "<img src=/erweima/$date/{$id} width='100%'/>";
        return Response::create($content, 'html');
    }

    public function uploadOne($img = 'image', $type = 'thumb')
    {
        $file = request()->file($img);
        if (!empty($file)) {

            // 移动到框架应用根目录/public/uploads/ 目录下
            $upload = $file->move(ROOT_PATH . 'public' . DS . 'erweima');
            // halt($upload);
            if ($upload) {

                if ($type == 'thumb') {
                    //图片处理
                    //imgDispose('public' . DS . 'uploads' . DS . $upload->getSaveName());
                }

                return $upload->getFilename();
                //返回图片地址
               // return '/erweima/' . date('Ymd') . '/' . $upload->getFilename();
            }
            return $file->getError();
        }

        return request()->param($img,'');
    }
}
